class HiringSpecialistController < ApplicationController

  def home
    require 'net/http'

    @pInfo = Array.new

    source = 'http://140.211.68.130/GetApplication'
    resp = Net::HTTP.post_form(URI.parse(source), 'state' => '1')
    data = resp.body
    result = JSON.parse(data)

    @applications = result['questions']

    @applications.each do |app|
      @pInfo.push(JSON.parse(app['application_data']))
    end

    source = 'http://140.211.68.130/GetOpenPositions'
    resp = Net::HTTP.get_response(URI.parse(source))
    data = resp.body
    result = JSON.parse(data)

    @open = result['positions']
    session[:open_positions] = @open

  end #end home

  def interview
    require 'net/http'

    @questions = Array.new
    @applyPosition = Array.new
    @personalInfo = JSON.parse(params[:appPersonalInfo])
    @appId = params[:app_id]
    positions = session[:open_positions]

    positions.each do |pos|
      if( pos['posting_id'] == @personalInfo['post_id'] ) then
        @applyPosition.push(pos)
        break
      end #end if
    end #end loop

    source = 'http://140.211.68.130/GetQuestions?answers=1'
    resp = Net::HTTP.post_form(URI.parse(source), 'position_type_id' => @personalInfo['position_id'])
    data = resp.body
    result = JSON.parse(data)

    @all = result['questions']

    @all.each do |question|
      if( question['application_state'] == '2' ) # 1 = kiosk, 2 = phone
        @questions.push(question)
      end #end if
    end #end loop

  end #end interview

  def app_submit
    require 'json'
    require 'net/http'

    status = params[:commit]
    application = Hash.new
    application['application_id'] = params[:app_id]
    application['notes'] = params[:notes]
    answers = Array.new

    if (status == 'Approve')
      application['state'] = '2'
    else  # DENIED!
      application['state'] = '0'
    end #end if

    params.each do |key, value|
      if( Integer(key) != nil rescue false )
        if ( Integer(value) != nil rescue false )
          answers.push({ 'position_question_id' => key, 'question_option_id' => value })
        else
          answers.push({ 'position_question_id' => key, 'answer_text' => value })
        end
      end
    end #end loop

    application['answers'] = answers.to_json

    source = 'http://140.211.68.130/SetApplication'
    resp = Net::HTTP.post_form(URI.parse(source), application)
    #data = resp.body
    #result = JSON.parse(data)

  end #end app_submit

end #end controller
